package com.syscom.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syscom.admin.service.SysCodeService;
import com.syscom.common.base.BasePage;
import com.syscom.common.entity.SysCode;
import com.syscom.common.utils.result.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @author wjz
 * @Date 2020-12-22
 */
@RestController
@RequestMapping("/sysCode")
public class SysCodeController {

    @Autowired
    private SysCodeService sysCodeService;

    @ApiOperation("SysCode查询接口")
    @GetMapping("/querySysCode")
    public AjaxResult querySysCode(BasePage basePage, Long parentId, Date startDate, Date endDate, String name, String tag) {

        QueryWrapper<SysCode> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(StrUtil.isNotEmpty(name), "name", name)
                .eq(StrUtil.isNotEmpty(tag), "tag", tag)
                .between(startDate != null, "created_date", startDate, endDate)
                .eq("parent_id", parentId).orderByAsc("order_no,object_id");
        return sysCodeService.querySysCode(basePage, queryWrapper);
    }

    @ApiOperation("SysCode新增接口")
    @PostMapping("/addSysCode")
    public AjaxResult addSysCode(@RequestBody SysCode sysCode) {

        return sysCodeService.addSysCode(sysCode);
    }

    @ApiOperation("SysCode更新接口")
    @PatchMapping("/updateSysCode/{objectId}")
    public AjaxResult updateSysCode(@PathVariable Long objectId, @RequestBody SysCode sysCode) {

        sysCode.setObjectId(objectId);
        return sysCodeService.updateSysCode(sysCode);
    }

    @ApiOperation("SysCode删除接口")
    @DeleteMapping("/deleteSysCode/{objectId}")
    public AjaxResult deleteSysCode(@PathVariable Long objectId) {

        return sysCodeService.deleteSysCode(objectId);
    }

}

